﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sun.Mvc.Models;
using Sun.Mvc.IDAL;

namespace Sun.Mvc.DAL
{
    public class Users : IUsers
    {
        Sun_CmsEntities db = new Sun_CmsEntities();

        /// <summary>
        /// 根据账号和密码获取用户信息
        /// </summary>
        /// <param name="model">用户</param>
        /// <returns></returns>
        public Models.Users GetUserInfoByAccount(Models.Users model)
        {
            if(db.Users.Any(a=> a.UserName == model.UserName && a.PassWord == model.PassWord))
            {
                return db.Users.Single(a => a.UserName == model.UserName && a.PassWord == model.PassWord);
            }

            return null;
        }

        /// <summary>
        /// 根据账号获得用户的私盐
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <returns></returns>
        public string GetSaltByAccount(string UserName)
        {
            if (db.Users.Any(a => a.UserName == UserName))
            {
                return db.Users.Single(a => a.UserName == UserName).Salt;
            }

            return null;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="model">用户</param>
        /// <returns></returns>
        public int Insert(Models.Users model)
        {
            if (!db.Users.Any(a => a.UserName == model.UserName))
            {
                db.Users.Add(model);
                return db.SaveChanges();
            }

            return 0;
        }
    }
}
